package org.mayanjun.code.easyweb.common;


import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * This class is a simple java bean that holds the status and data which to be
 * serialized by view resolver
 * 
 * @author mayanjun
 */
public class Result implements Serializable {
	
	public static final String KEY_ENTITY = "entity";
	public static final String KEY_PAGING = "paging";
	public static final String KEY_LIST = "list";

	private static final long serialVersionUID = -1206732821700623440L;

	private Map<String, Object> data;

	private Status status;
	
	private String exception;

	public Result() {
		this(Status.OK);
	}

	public Result(Status status) {
		this.status = status;
	}

	public static Result ok() {
		return new Result();
	}

	public Map<String, Object> getData() {
		return data;
	}

	public Result add(String key, Object value) {
		checkData();
		this.data.put(key, value);
		return this;
	}
	
	private void checkData() {
		if(data == null) this.data = new HashMap<String, Object>();
	}

	public Result addAll(Map<String, ?> map) {
		checkData();
		this.data.putAll(map);
		return this;
	}

	public Result remove(String key) {
		if(data != null) {
			this.data.remove(key);
		}
		return this;
	}
	
	public int getCode() {
		return this.status.getCode();
	}
	
	public String getMsg() {
		return this.status.getMessage();
	}

	public String getException() {
		return exception;
	}

	public Result setException(String exception) {
		this.exception = exception;
		return this;
	}
}